/****************************** Module Header ******************************\
 * Module Name:  DBPagger.cs
 * Module Version:  1.0.0 (2011-9-22)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to collections object's pagging. It supplies the pagger
 * object's attributes and pagging method.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UCF.Core
{
    /// <summary>
    /// 数据库泛型分页类
    /// <para>提供对IList对象的分页功能</para>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PaggerGeneric<T> : IPaggerGeneric<T>
    {
        /// <summary>
        /// 第一页页码
        /// </summary>
        public int FirstPage { get { return 1; } }

        /// <summary>
        /// 上一页页码
        /// </summary>
        public int PrevPage
        {
            get
            {
                return (this.PageIndex > 1) ? this.PageIndex - 1 : this.PageIndex;
            }
        }

        /// <summary>
        /// 下一页页码
        /// </summary>
        public int NextPage
        {
            get
            {
                return (this.PageIndex < this.PageCount) ? this.PageIndex + 1 : this.PageIndex;
            }
        }

        /// <summary>
        /// 最末页页码
        /// </summary>
        public int LastPage { get { return this.PageCount; } }

        /// <summary>
        /// 当前页页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 当前页列表中游标位置
        /// </summary>
        public int ListIndex { get; private set; }

        /// <summary>
        /// 记录集中游标位置
        /// </summary>
        public int RecordIndex { get; private set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; private set; }

        /// <summary>
        /// 分页尺寸
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; private set; }

        /// <summary>
        /// 当前页记录数
        /// </summary>
        public int ListCount { get; private set; }

        /// <summary>
        /// 当前页数据集
        /// </summary>
        public IList<T> ListSet { get; private set; }

        /// <summary>
        /// 记录集
        /// </summary>
        protected IEnumerable<T> RecordSet;

        /// <summary>
        /// URL分页变量名称
        /// </summary>
        public string PageVarName { get; set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        public PaggerGeneric(int PageIndex, int PageSize)
        {
            this.PageIndex = PageIndex < 1 ? 1 : PageIndex;
            this.PageSize = PageSize < 1 ? 1 : PageSize;
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        public PaggerGeneric()
        {

        }

        /// <summary>
        /// 绑定数据源
        /// </summary>
        /// <param name="List"></param>
        public void BindDataSource(IEnumerable<T> List)
        {
            if (this.PageSize == 0)
            {
                this.ListSet = List.ToList();
                return;
            }
            this.RecordSet = List;
            this.RecordCount = List.Count();
            this.PageCount = this.RecordCount / this.PageSize;
            if (this.RecordCount % this.PageSize > 0) this.PageCount += 1;

            if (this.PageIndex < 1) this.PageIndex = 1;
            this.ListCount = this.PageSize;
            int firstIndex = this.PageSize * (this.PageIndex - 1);
            int lastIndex = firstIndex + this.PageSize;
            if (lastIndex > this.RecordCount)
            {
                lastIndex = this.RecordCount;
                this.ListCount = lastIndex - firstIndex;
            }
            this.RecordIndex = firstIndex;

            ListSet = new List<T>();
            for (int i = firstIndex; i < lastIndex; i++)
            {
                ListSet.Add(List.ElementAt(i));
            }


        }

        /// <summary>
        /// 移动游标到下一条记录
        /// </summary>
        /// <returns></returns>
        public bool Next()
        {
            bool blnNext = false;
            if (this.RecordCount > 0 && this.ListIndex < this.ListCount)
            {
                blnNext = true;
                this.ListIndex += 1;
                this.RecordIndex += 1;
            }
            return blnNext;
        }

        /// <summary>
        /// 获取当前记录对象
        /// </summary>
        /// <returns></returns>
        public T Fetch()
        {
            T obj = ListSet[this.ListIndex - 1];
            return obj;
        }
    }
}
